﻿/* ============================================================================== 
2  * NET Framework：4.0.30319.17929
3  * 创 建 者：JDD01-PC.jdd01.zhouwei 
4  * 创建日期：2014/1/24 17:50:30  
5  * 功能说明：
6  * 引用说明：
* ==============================================================================*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Data;
namespace ConsoleApplication7
{
	class DataTableExploer
	{
		//*******
		//作者:        wesimy
		//时间:        07/31/05
		//版本:        v.2.1
		//函数名:     DatatableToCSVFile
		//参数说明:   DataTable dt 导出CSV的数据
		//                string xbkPahth    CSV模板,主要存储一些表头的格式
		//                string SavePath    导出的路径
		//                ref string err        出错提示
		//功能:         把DataTable的数据导出到CSV文件中

		//********//

		public static void DatatableToCSVFile(System.Data.DataTable dt, string xbkPath, string SavePath)
		{
			string row;
			try
			{
				if (!Directory.Exists(Path.GetDirectoryName(SavePath)))
				{
					Directory.CreateDirectory(Path.GetDirectoryName(SavePath));
				}
				if (File.Exists(SavePath))
				{
					File.Delete(SavePath);
				}
				//string header;
				string tmp;
				//StreamReader sr = new StreamReader(xbkPath);
				//header = sr.ReadLine();

				//sr.Close();
				FileStream fs = File.Create(SavePath);
				StreamWriter sw = new StreamWriter(fs);
				//if (header!="")
				//{
				//        sw.WriteLine(header);
				//}


				foreach (DataRow dr in dt.Rows)
				{
					row = "";
					for (int i = 0; i < dt.Columns.Count; i++)
					{
						if (i != dt.Columns.Count - 1)
						{
							tmp = dr[i].ToString().Trim().Replace(",", " ");
							row = row + tmp + ",";
						}
						else
						{
							tmp = dr[i].ToString().Trim().Replace(",", ".");
							row = row + tmp;
						}
					}
					sw.WriteLine(row);
				}
				sw.Flush();
				sw.Close();
			}
			catch (Exception ex)
			{
				Clog.Write(DateTime.Now.ToString("HHmmss"), ex.ToString());
				//	err = ex.ToString();

			}
		}
		public static void DatatableToCSVFile(System.Data.DataTable dt, string SavePath)
		{
			if (dt == null || dt.Rows.Count == 0)
			{
				return;
			}
			if (dt.Rows.Count > 10000)
			{
				int pageouct = dt.Rows.Count / 10000 + (dt.Rows.Count % 10000 != 0 ? 1 : 0);
				
				for (int i = 0; i < pageouct; i++)
				{
					int start = i * 10000;
					Console.WriteLine(SavePath+ "... "+start.ToString());
					//int end = (i + 1) * 10000;
					//if (end>dt.Rows.Count)
					//{
					//    end = 10000;
					//    //break;
					//}
					DataTable dt1 = GetDT(dt, start);
					DatatableToCSVFile(dt1, "", Path.GetDirectoryName(SavePath) + "\\" + i.ToString() + Path.GetFileName(SavePath));
				}
			}
			else {
				Console.WriteLine(SavePath);
				DatatableToCSVFile(dt, "", Path.GetDirectoryName(SavePath) + "\\"  + Path.GetFileName(SavePath));
			}
		}
		static DataTable GetDT(DataTable dt, int start)
		{
			int pagesize = 10000;
			if (start + pagesize > dt.Rows.Count)
			{
				pagesize = dt.Rows.Count - start;
			}
			IEnumerable<DataRow> query = dt.AsEnumerable().Skip(start).Take(pagesize);
			DataTable dt1 = query.CopyToDataTable();
			return dt1;
		}
	}

}

